package fr.upmc.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.upmc.utils.SessionTracker;

public class DispatcherOption extends HttpServlet {

	private static final long serialVersionUID = 491910053496068545L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {

		//if user not connected, get him the hell out of here
		if(!SessionTracker.isUserConnected(request))
		{
			SessionTracker.redirectToHome(response);
			return;
		}
		
		String operation = request.getParameter("opinions");
		RequestDispatcher rd = null;

		if(operation.equals("addContact")){
			rd = request.getRequestDispatcher("addContact.jsp");
		}
		else if(operation.equals("deleteContact")){
			rd = request.getRequestDispatcher("deleteContact.jsp");
		}
		else if(operation.equals("updateContact")){
			rd = request.getRequestDispatcher("updateContact.jsp");
		}
		else if(operation.equals("searchContact")){
			rd = request.getRequestDispatcher("searchContact.jsp");
		}
		else if(operation.equals("listContacts")){
			rd = request.getRequestDispatcher("allContacts.jsp");
		}
		else{
			rd = request.getRequestDispatcher("accueil.jsp");
		}

		rd.forward(request, response);

		return;
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {

		doGet(request, response);
		return;
	}
}
